home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CLRCTR10.ARJ / CLRTEST.C < prev    next >
C/C++ Source or Header  |  1991-06-15  |  8KB  |  384 lines

  1. /*
  2.  
  3. Main application module for test of color selector custom control
  4.  
  5. (C) Scott Gourley, 1991.
  6.  
  7. */
  8.  
  9. #include <windows.h>
  10.  
  11. #include "clrctrl.h"
  12. #include "clrtest.h"
  13.  
  14. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  15.  
  16. COLORREF    clrText = CLRTEST_TEXT_COLOR;
  17. COLORREF    clrBack = CLRTEST_BACKGROUND_COLOR;
  18.  
  19. int PASCAL WinMain
  20.     (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  21. {
  22.     WNDCLASS    wndclass;
  23.     HANDLE        hClrLib;
  24.     HANDLE        hAccel;
  25.     HWND        hWnd;
  26.     MSG            msg;
  27.     static char    szAppName [] = "ClrTest";
  28.  
  29.     /* load custom control DLL library, end program on error */
  30.     
  31.     if ((hClrLib = LoadLibrary (CLRCTRL_DLLNAME)) < 32)
  32.         return 0;
  33.  
  34.     /* register the window class if this is the only program instance */
  35.     
  36.     if (!hPrevInstance)
  37.     {
  38.         /* register the frame window class */
  39.  
  40.         wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  41.         wndclass.lpfnWndProc    = WndProc;
  42.         wndclass.cbClsExtra        = 0;
  43.         wndclass.cbWndExtra        = 0;
  44.         wndclass.hInstance        = hInstance;
  45.         wndclass.hIcon            = LoadIcon (hInstance, szAppName);
  46.         wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  47.         wndclass.hbrBackground    = NULL;
  48.         wndclass.lpszMenuName    = szAppName;
  49.         wndclass.lpszClassName    = szAppName;
  50.     
  51.         RegisterClass (&wndclass);        
  52.     }
  53.  
  54.     /* create the application window */
  55.  
  56.     hWnd = CreateWindow (szAppName, "Color Selector Test", 
  57.                                 WS_OVERLAPPEDWINDOW,
  58.                                 CW_USEDEFAULT, CW_USEDEFAULT,
  59.                                 240, 100,
  60.                                 NULL, NULL, hInstance, NULL);
  61.  
  62.     /* load the keyboard accelerators */
  63.                             
  64.     hAccel = LoadAccelerators (hInstance, "Accelerators");
  65.     
  66.     /* make the frame window visible */
  67.     
  68.     ShowWindow (hWnd, nCmdShow);
  69.     UpdateWindow (hWnd);
  70.     
  71.     /* enter the message loop */
  72.     
  73.     while (GetMessage (&msg, NULL, 0, 0))
  74.     {
  75.         /* if there is no accel key to translate, process the message */
  76.         
  77.         if (!hAccel || !TranslateAccelerator (hWnd, hAccel, &msg))
  78.         {
  79.             TranslateMessage (&msg);
  80.             DispatchMessage (&msg);
  81.         }
  82.     }
  83.     
  84.     FreeLibrary (hClrLib);
  85.  
  86.     return msg.wParam;
  87. }
  88.  
  89. BOOL FAR PASCAL ClrTestDlgProc
  90.     (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  91. {
  92.     HWND            hClrCtrl;
  93.  
  94.     switch (wMsg)
  95.     {
  96.         case WM_INITDIALOG:
  97.  
  98.             /* get a handle to the text color selector */
  99.             
  100.             hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_TEXT);
  101.  
  102.             /* delete the dark magenta item, just for fun */
  103.  
  104.             SendMessage (hClrCtrl, CB_DELETESTRING, 5, 0L);
  105.             
  106.             /* put a sky-blue item at position 3, just for fun */
  107.  
  108.             SendMessage (hClrCtrl, CB_INSERTSTRING, 3,
  109.                 RGB (0x00, 0x80, 0xFF));
  110.             
  111.             /* set the default text color (to clrText's initial value) */
  112.  
  113.             SendMessage (hClrCtrl, CLRM_SETCURCOLOR, 0, clrText);
  114.  
  115.             /* get a handle to the background color selector */
  116.  
  117.             hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND);
  118.  
  119.             /* add an orangish item to the end of the list, just for fun */
  120.             
  121.             SendMessage (hClrCtrl, CB_ADDSTRING, 0,
  122.                 RGB (0xC0, 0x40, 0x00));
  123.             
  124.             /* set the default background color (to clrBack's init value) */
  125.  
  126.             SendMessage (hClrCtrl, CLRM_SETCURCOLOR, 0, clrBack);
  127.  
  128.             return TRUE;
  129.  
  130.             break;
  131.             
  132.         case WM_COMMAND:
  133.             switch (wParam)
  134.             {
  135.                 case IDOK:
  136.  
  137.                     /* access the text color selector... */
  138.  
  139.                     hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_TEXT);
  140.                     
  141.                     /* ...and retrieve the current color setting */
  142.  
  143.                     clrText = SendMessage (hClrCtrl, CLRM_GETCURCOLOR, 0, 0L);
  144.  
  145.                     /* access the background color selector... */
  146.                     
  147.                     hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND);
  148.                     
  149.                     /* ...and retrieve the current color setting */
  150.  
  151.                     clrBack = SendMessage (hClrCtrl, CLRM_GETCURCOLOR, 0, 0L);
  152.  
  153.                     /* fall through to end the dialog */
  154.  
  155.                 case IDCANCEL:
  156.  
  157.                     /* end the dialog */
  158.                     
  159.                     EndDialog (hDlg, 0);
  160.  
  161.                     return TRUE;
  162.  
  163.                 case ID_CLRSEL_RESET:
  164.  
  165.                     /* reset the current colors to the default values */
  166.  
  167.                     clrText = CLRTEST_TEXT_COLOR;
  168.                     clrBack = CLRTEST_BACKGROUND_COLOR;
  169.  
  170.                     /* reset the current text color selection */
  171.                     
  172.                     SendMessage (GetDlgItem (hDlg, ID_CLRSEL_TEXT),
  173.                         CLRM_SETCURCOLOR, 0, clrText);
  174.                     
  175.                     /* reset the current background color selection */
  176.  
  177.                     SendMessage (GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND),
  178.                         CLRM_SETCURCOLOR, 0, clrBack);
  179.  
  180.                     break;
  181.             }
  182.             break;
  183.     }
  184.     
  185.     return FALSE;
  186. }
  187.  
  188. BOOL FAR PASCAL AboutDlgProc
  189.     (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  190. {
  191.     switch (wMsg)
  192.     {
  193.         case WM_COMMAND:
  194.  
  195.             switch (wParam)
  196.             {
  197.                 case IDCANCEL:
  198.                 case IDOK:
  199.  
  200.                     EndDialog (hDlg, 0);
  201.  
  202.                     return TRUE;
  203.                     
  204.                     break;
  205.             }
  206.  
  207.             break;
  208.     }
  209.             
  210.     return (FALSE);
  211. }
  212.  
  213. LONG FAR PASCAL WndProc
  214.     (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  215. {
  216.     static FARPROC    lpfnClrTestDlgProc;
  217.     static FARPROC    lpfnAboutDlgProc;
  218.     static HANDLE    hInstance;
  219.     
  220.     switch (wMsg)
  221.     {
  222.         /* create the application window */
  223.         
  224.         case WM_CREATE:
  225.             /* save the program's instance handle */
  226.             
  227.             hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
  228.             
  229.             /* create instance "thunks" for the dialog box procedures */
  230.         
  231.             lpfnClrTestDlgProc =
  232.                 MakeProcInstance (ClrTestDlgProc, hInstance);
  233.             lpfnAboutDlgProc =
  234.                 MakeProcInstance (AboutDlgProc, hInstance);
  235.  
  236.             return 0;
  237.             
  238.         case WM_COMMAND:
  239.             switch (wParam)
  240.             {
  241.                 case IDM_EXIT:
  242.  
  243.                     /* end the application */
  244.  
  245.                     SendMessage (hWnd, WM_CLOSE, 0, 0L);
  246.  
  247.                     return 0;
  248.  
  249.                     break;
  250.                     
  251.                 case IDM_CLRTEST:
  252.  
  253.                     /* put up the dial test dialog box */
  254.  
  255.                     DialogBox (hInstance, "ClrTest",
  256.                         hWnd, lpfnClrTestDlgProc);
  257.  
  258.                     /* repaint the window, to get the new selected colors */
  259.                     
  260.                     InvalidateRect (hWnd, NULL, TRUE);
  261.  
  262.                     return 0;
  263.                     
  264.                     break;
  265.  
  266.                 case IDM_INDEXHELP:
  267.                     
  268.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_INDEX, 0L);
  269.  
  270.                     break;
  271.                 
  272.                 case IDM_COMMANDHELP:
  273.  
  274.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_CONTEXT, 1L);
  275.                     
  276.                     break;
  277.  
  278.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_INDEX, 0L);
  279.  
  280.                     break;
  281.                 
  282.                 case IDM_USINGHELP:
  283.  
  284.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_HELPONHELP, 0L);
  285.  
  286.                     break;
  287.                 
  288.                 case IDM_ABOUT:
  289.  
  290.                     /* put up the dial test about dialog box */
  291.  
  292.                     DialogBox (hInstance, "About",
  293.                         hWnd, lpfnAboutDlgProc);
  294.  
  295.                     /* repaint the window, to get the new selected colors */
  296.                     
  297.                     InvalidateRect (hWnd, NULL, TRUE);
  298.  
  299.                     return 0;
  300.                     
  301.                     break;
  302.             }
  303.  
  304.             break;
  305.  
  306.         case WM_PAINT:
  307.         {
  308.             PAINTSTRUCT    ps;
  309.             HBRUSH        hBrush;
  310.             HPEN        hPen;
  311.             RECT        rect;
  312.             HDC            hDC;
  313.  
  314.             /* start painting, and get a device context */
  315.             
  316.             hDC = BeginPaint (hWnd, &ps);
  317.  
  318.             /* determine the size of the application window to paint on */
  319.             
  320.             GetClientRect (hWnd, &rect);
  321.  
  322.             /* set the appropriate text and text background colors */
  323.             
  324.             SetTextColor (hDC, clrText);
  325.             SetBkColor (hDC, clrBack);
  326.             SetBkMode (hDC, TRANSPARENT);
  327.             
  328.             /* create solid colored brush and pen to paint the background */
  329.             
  330.             hBrush = CreateSolidBrush (clrBack);
  331.             hPen = CreatePen (PS_INSIDEFRAME, 2, clrBack);
  332.  
  333.             /* select the brush and pen into the DC, saving the old values */
  334.  
  335.             hBrush = SelectObject (hDC, hBrush);
  336.             hPen = SelectObject (hDC, hPen);
  337.             
  338.             /* paint the window with the background color */
  339.             
  340.             Rectangle (hDC, rect.left, rect.top, rect.right, rect.bottom);
  341.  
  342.             /* paint the text, centered in the window, in the correct color */
  343.             
  344.             DrawText (hDC, (LPSTR) "Change colors using Options!", -1, &rect,
  345.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  346.  
  347.             /* restore the old brush and pen */
  348.             
  349.             hBrush = SelectObject (hDC, hBrush);
  350.             hPen = SelectObject (hDC, hPen);
  351.             
  352.             /* delete the created brush */
  353.             
  354.             DeleteObject (hBrush);
  355.             DeleteObject (hPen);
  356.             
  357.             /* end the painting */
  358.             
  359.             EndPaint (hWnd, &ps);
  360.  
  361.             return 0;
  362.  
  363.             break;
  364.         }
  365.  
  366.         case WM_DESTROY :
  367.             
  368.             /* close the help utility */
  369.             
  370.             WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_QUIT, 0L);
  371.  
  372.             /* end the program */
  373.  
  374.             PostQuitMessage (0);
  375.  
  376.             return 0;
  377.     }
  378.     
  379.     /* hand all unprocessed messages to the default window-proc in Windows */
  380.     
  381.     return DefWindowProc (hWnd, wMsg, wParam, lParam);
  382. }
  383.  
  384.